﻿using Microsoft.CodeAnalysis;
using System;

namespace DimensionsHelper.SourceGeneration
{

    public readonly struct DiagnosticInfo
    {

        public DiagnosticInfo(DiagnosticDescriptor descriptor, object[] messageArgs, Location location)
        {
            Descriptor = descriptor;
            MessageArgs = messageArgs;
            Location = location;
        }


        public DiagnosticDescriptor Descriptor { get; }

        public object[] MessageArgs { get; }

        public Location Location { get; }


        public Diagnostic GetDiagnostic()
        {
            return Diagnostic.Create(Descriptor, Location, MessageArgs);
        }


        public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location location, object[] messages)
        {
            Location loc = location is null ? null : Location.Create(location.SourceTree?.FilePath ?? "", location.SourceSpan, location.GetLineSpan().Span);
            return new DiagnosticInfo(descriptor, messages ?? Array.Empty<object>(), loc);
        }

    }

}